package com.jxs.tourist.controller;

import com.jxs.tourist.domain.News;
import com.jxs.tourist.service.NewsService;
import com.jxs.tourist.util.FileUpload;
import com.jxs.tourist.util.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/news")
public class NewsController {
    public NewsController(){
        System.out.println("init");
    }
    @Resource
    NewsService newsService;
    /**
     * 添加
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="/addNews")
    public JsonResult<String> addNews(HttpServletRequest request){
        News news=new News();
        news.setAddtime(new Date());
        news.setContent(request.getParameter("content"));
        news.setTitle(request.getParameter("title"));
        news.setGallery(request.getParameter("imgs"));
        if(newsService.addNews(news)==1){
            return new JsonResult<String>("","添加成功",0);
        }
        return new JsonResult<String>("","添加失败",1);
    }
    /**
     * 编辑
     * @param file
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value ="/editNews")
    public JsonResult<String> editNews( HttpServletRequest request){
        int news_id=Integer.valueOf(request.getParameter("news_id"));
        News news=newsService.getNewsById(news_id);
        if(news==null){
            return new JsonResult<String>("","不存在",1);
        }
        String title=request.getParameter("title");
        String content=request.getParameter("content");
        news.setContent(content);
        news.setTitle(title);
        news.setGallery(request.getParameter("imgs"));
        if(newsService.editNews(news)==1){
            return new JsonResult<String>("","编辑成功",0);
        }
        return new JsonResult<String>("","编辑失败",1);
    }

    /**
     * 删除
     * @param news_id
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteNews")
    public JsonResult<String> deleteNews(@RequestParam("news_id") int news_id,HttpServletRequest request){
        if(newsService.deleteNewsById(news_id)==1){
            return new JsonResult<String>("","删除成功",0);
        }
        return new JsonResult<String>("","删除失败",1);
    }
    /**
     * 查询所有
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllNews",method = RequestMethod.POST)
    public JsonResult<List<News>> getAllNews(HttpServletRequest request) {
        List<News> list = newsService.getAllNews();
        JsonResult<List<News>> result;
        if (list == null || list.size() == 0) {
            result = new JsonResult<List<News>>(list, "暂时没有数据", 1);
        } else {
            result = new JsonResult<List<News>>(list, "获取成功", 0);
        }
        return result;
    }
}
